﻿using System;
using System.Collections.Generic;

using aier.dataaccess.model;
using aier.dataaccess.dapper;
using aier.xrm.wcf;

using Microsoft.Xrm.Sdk;
using log4net;

namespace aier.business.logic
{
    public class SchoolBll
    {
        public ILog log { get; set; }
        public ResResult resResult { get; set; }
        public SchoolDal schoolDal { get; set; }
        public CommonWcf commonWcf { get; set; }
        public ContactBll contactBll { get; set; }
        public XrmOptionDal xrmOptionDal { get; set; }

        /// <summary>
        /// 通过视图获取所有学校信息
        /// </summary>
        /// <returns></returns>
        public List<SchoolView> QueryAllSchool()
        {
            return schoolDal.QueryAllSchool();
        }

        ///// <summary>
        ///// 获取学校等级选项集
        ///// </summary>
        ///// <returns></returns>
        //public IEnumerable<XrmOption> QuerySchoolLevel() {
        //    return xrmOptionDal.GetXrmOptionByEntity("new_school", "new_level");
        //}

        

        /// <summary>
        /// 根据学校ID查询视图信息
        /// </summary>
        /// <param name="shoolId"></param>
        /// <returns></returns>
        public SchoolView QuerySchoolById(string shoolId)
        {
            //return new CommunityDal().QueryCommunityById(communityId);
            return schoolDal.QuerySchoolId(shoolId);
        }

        /// <summary>
        /// 根据学校名称查询视图信息
        /// </summary>
        /// <param name="schoolName"></param>
        /// <returns></returns>
        public List<SchoolView> QuerySchoolByName(string schoolName)
        {
            return schoolDal.QuerySchoolByName(schoolName);
        }

        /// <summary>
        /// 根据省市区/县ID查询学校视图信息
        /// </summary>
        /// <param name="provice"></param>
        /// <param name="city"></param>
        /// <param name="county"></param>
        /// <returns></returns>
        public List<SchoolView> QuerySchoolByProviceCityCounty(string provice, string city, string county,string hospital)
        {
           
            city = city == "全部" ? string.Empty : city;
            county = county == "全部" ? string.Empty : county;
            return schoolDal.QuerySchoolByProviceCityCounty(provice, city, county, hospital);
        }


        /// <summary>
        /// 根据不同查询条件查询学校对像,只要属性不为空就视为查询条件
        /// 原程序 querySchool
        /// </summary>
        /// <param name="school">学校对像</param>
        /// <returns></returns>
        public IEnumerable<SchoolView> QuerySchool(SchoolView school)
        {
            if(school.new_cityName== "全部")
            {
                school.new_cityName = string.Empty;
            }
            if(school.new_areaName == "全部")
            {
                school.new_areaName = string.Empty;
            }
            return schoolDal.QuerySchool(school);
        }

        /// <summary>
        /// 根据不同查询统计学校对像数量,
        /// 查询条件为：省ID,市ID,区/县ID,街道ID,学校ID,学校名称
        /// </summary>
        /// <param name="school">学校对像</param>
        /// <returns></returns>
        public int QuerySchoolCount(SchoolView school)
        {
            return schoolDal.QuerySchoolCount(school);
        }

        #region 更新

        /// <summary>
        /// 创建学校
        /// </summary>
        /// <param name="school"></param>
        /// <returns></returns>
        public ResResult CreateSchool(School school)
        {
            Guid entityId = Guid.Empty;
            Entity entity = school.AsCrmEntity();

            try
            {
                if (Guid.Empty.Equals(school.userid))
                {
                    entityId = commonWcf.CreateEntity(entity);
                }
                else
                {
                    entityId = commonWcf.CreateEntityProxy(school.userid, entity);
                }

                return resResult.SuccessResult(entityId);
            }
            catch (Exception ex)
            {
                log.Error("ScreenBll-> CreateSchool: " + ex.Message);
                return resResult.FailResult(ex.Message);
            }
        }

        /// <summary>
        /// 根据学校ID修改学校信息
        /// </summary>
        /// <param name="school"></param>
        /// <returns></returns>
        public ResResult UpdateSchoolById(School school)
        {
            try
            {
                Entity entity = school.AsCrmEntity();
                if (Guid.Empty.Equals(school.userid))
                {
                    commonWcf.UpdateEntityById(entity);
                }
                else
                {
                   commonWcf.UpdateEntityProxyById(school.userid, entity);
                }

                return resResult.SuccessResult();
            }
            catch (Exception ex)
            {
                log.Error("ScreenBll-> UpdateSchoolById: " + ex.Message);
                return resResult.FailResult(ex.Message);
            }
        }

        
        #endregion

    }
}
